import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import Settle from '../components/Settle/Settle.vue'
import Welcome from '../components/Welcome.vue'
import Cochain from '../components/Cochain/Cochain.vue'
import Identification from '../components/Identification/Identification.vue'
import CompileInfo from '../components/Identification/compileInfo'
import Process from '../components/Cochain/process'
import Work from '../components/Work/Work.vue'
import addWork from '../components/Work/addWork.vue'
import axios from 'axios'
import { getToken } from '@/utils/auth'



Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'hash',
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/settle', component: Settle },
                { path: '/cochain', component: Cochain },
                { path: '/identification', component: Identification },
                { path: '/work', component: Work },
                { path: '/addWork', component: addWork },
                { path: '/process', component: Process },
                { path: '/compileInfo', component: CompileInfo }
            ]
        },
    ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    axios.defaults.headers.common['token'] = getToken('Token')
        // to:将要访问的路径
        // from:从哪里访问的路径
        // next:之后要做的任务，是一个函数
        //    next（）放行， next（'/URL'）强制跳转的路径。
    if (to.path == '/login') return next(); // 访问路径为登录
    // 获取flag
    const flagStr = window.sessionStorage.getItem("acess"); // session取值
    if (!flagStr) return next(); // 没登录去登录
    next();
})

export default router // 暴露出去